package com.xs.designpattern;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 这是外观类，封装了矮人在金矿中的各种操作。
 *
 * @author xs
 */
public class DwarvenGoldmineFacade {
    private final List<DwarvenMineWorker> workers;

    /**
     * 在构造函数中初始化了矮人工人列表
     */
    public DwarvenGoldmineFacade() {
        workers = Arrays.asList(new DwarvenGoldDigger(), new DwarvenCartOperator(), new DwarvenTunnelDigger());
    }

    public void startNewDay() {
        makeActions(workers, DwarvenMineWorker.Action.WAKE_UP, DwarvenMineWorker.Action.GO_TO_MINE);
    }

    public void digOutGold() {
        makeActions(workers, DwarvenMineWorker.Action.WORK);
    }

    public void endDay() {
        makeActions(workers, DwarvenMineWorker.Action.GO_HOME, DwarvenMineWorker.Action.GO_TO_SLEEP);
    }

    /**
     * 使用私有的 makeActions() 方法来批量执行操作
     *
     * @param workers 工人
     * @param actions 操作类型
     */
    private static void makeActions(Collection<DwarvenMineWorker> workers, DwarvenMineWorker.Action... actions) {
        workers.forEach(worker -> worker.action(actions));
    }
}
